package com.zhs.study.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhs.study.dao.UserRoleDao;
import com.zhs.study.dto.UserRoleDto;
import com.zhs.study.model.UserRole;
import com.zhs.study.service.IUserRoleService;

import com.zhs.study.common.Enum.R;
import com.zhs.study.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import javax.annotation.Resource;


/**
 *
 * @author
 * @since 2023-12-19
 */
@Service
@Slf4j
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRole> implements IUserRoleService {

    @Resource
    private UserRoleDao userRoleDao;

    @Override
    public R addOrUpdate(UserRoleDto userRoleDto) {
        if (BeanUtil.isEmpty(userRoleDto)) throw new ServiceException("数据不能为空");
        UserRole userRole = new UserRole();
        BeanUtil.copyProperties(userRoleDto,userRole);
        if (userRoleDto.getId() == null){
            userRoleDao.insert(userRole);
            return R.ok("数据插入成功");
        }else {
            userRoleDao.updateById(userRole);
            return R.ok("数据更新成功");
        }
    }
}
